import { _decorator, Component, Node, Vec3,input,Input, EventMouse, labelAssembler } from 'cc';
import { SkeletalAnimation } from 'cc';
import { EventTarget } from 'cc';
import { Label } from 'cc';
const { ccclass, property } = _decorator;

//const eventMgr = new EventTarget();


@ccclass('PlayerController')
export class PlayerController extends Component {

    @property({type: SkeletalAnimation})
    public CocosAnim: SkeletalAnimation | null = null;

   

    private _curMoveCount: number = 0;

    private _startJump: boolean = false;

    // private _jumpStep: number = 0;
    // 当前跳跃时间
    private _curJumpTime: number = 0;
    // 每次跳跃时长
    private _jumpTime: number = 0.3;
    // 当前跳跃速度
    private _curJumpSpeed: number = 0;


    private _curPos: Vec3 = new Vec3();
    // // 每次跳跃过程中，当前帧移动位置差
    private _deltaPos: Vec3 = new Vec3(0, 0, 0);

    private _targetPos: Vec3 = new Vec3();

    // private _curMoveIndex = 0;

    start() {
        input.on(Input.EventType.MOUSE_UP, this.onMouseUp, this);
        //eventMgr.on("stepFinish_event",this.onStepFinish,this);
        this._curMoveCount = 0;
    }

    setInputActive(active: boolean){
        if(active){
            input.on(Input.EventType.MOUSE_UP, this.onMouseUp, this);
        } else {
            input.off(Input.EventType.MOUSE_UP, this.onMouseUp, this);
        }
    }

    // onStepFinish() {
    //     console.log("finish");
    //     //this.stepLabel.string = this._curMoveCount.toString(); 

    // }
    onMouseUp(event: EventMouse){
        if(this._startJump) {
            return;
        }
        
        this.node.getPosition(this._curPos);

        if(event.getButton() === 0){
            console.log("left\n");
            this.jumpByStep(1);
        }
        else if (event.getButton() === 2){
            console.log("right\n");
            this.jumpByStep(2);
           
        }
        this._startJump = true;
    }

    jumpByStep(step: number) {

        //1. 当前开始运动计时
        this._curJumpTime = 0;

        //2. 计算速度
        this._curJumpSpeed = step / this._jumpTime;

        //3. 计算最终目标位置
        this.node.getPosition(this._curPos);
        Vec3.add(this._targetPos,this._curPos,new Vec3(step,0,0));

       
        if(this.CocosAnim){
            
            this.CocosAnim.getState('cocos_anim_jump').speed = 3.5;
            this.CocosAnim.play('cocos_anim_jump');
           
        }

        this._curMoveCount +=step;
    }

    onOneceJumpEnd() {
        if(this.CocosAnim){
            this.CocosAnim.play('cocos_anim_idle');
        }
        
        //eventMgr.emit("stepFinish_event",this._curMoveCount);
        this.node.emit('JumpEnd',this._curMoveCount);
    }

    reset() {
        this._curMoveCount = 0;
        this.node.setPosition(0,0,0);
        
    }

    update(deltaTime: number) {
        if(this._startJump){
            this._curJumpTime += deltaTime;
            if(this._curJumpTime > this._jumpTime) {
                this.node.setPosition(this._targetPos);
                
                this.onOneceJumpEnd();
                this._startJump = false;
            } else {
                this.node.getPosition(this._curPos);
                this._deltaPos.x = this._curJumpTime * this._curJumpSpeed;
                Vec3.add(this._curPos, this._curPos, this._deltaPos);
                this.node.setPosition(this._curPos);
            }
        }
    }
    
}


